package com.vadmin.vadminjava.service;

import com.alibaba.fastjson.JSONObject;
import com.vadmin.vadminjava.common.ResultSetToJavaBean;
import com.vadmin.vadminjava.db.DBUtil;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;

/* **************************************************************************
 *
 *                              系统配置
 *
 * *************************************************************************/
@Service
public class AdminConfigService {


	org.slf4j.Logger logger = LoggerFactory.getLogger(AdminConfigService.class.getName());


	/**
	 * 系统配置列表
	 * @param params
	 * @return
	 */
	public JSONObject systemConfigList(JSONObject params) {
		JSONObject map = new JSONObject();
		map.put("resultFlag", false);
		DBUtil dbUtil = new DBUtil();
		//sql处理
		Integer page      = params.getInteger("page");
		Integer pageSize  = params.getInteger("pageSize");
		String startTime = params.getString("startTime");
		String endTime   = params.getString("endTime");

		String countSql = "SELECT COUNT(0) as count FROM system_configuration WHERE visibility=1 ";
		String sql = "select * from system_configuration where visibility=1  ";

		if(StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(startTime)){
			sql += " AND create_time >= '" + startTime + "' ";
			countSql += " AND create_time >= '" + startTime + "' ";
		}
		if(StringUtils.isNotEmpty(endTime) && StringUtils.isNotEmpty(endTime)) {
			sql += " AND create_time <= '" + endTime + "' ";
			countSql += " AND create_time <= '" + endTime + "' ";
		}


		sql += " order by k asc ";
		sql += " limit " + ((page - 1) * pageSize) + "," + pageSize;

		//执行
		try {
			ArrayList<JSONObject> objects = ResultSetToJavaBean.copyJson(dbUtil.executeQuery(sql));
			int                   count   = ResultSetToJavaBean.copyJsonSingleQuery(dbUtil,countSql).getInteger("count");


			map.put("count",count);
			map.put("data", objects);
			map.put("resultFlag", true);
			map.put("resultMsg", "操作成功");
		} catch (Exception e) {
			map.put("resultMsg", "操作失败");
		} finally {
			dbUtil.closeNoErr();
		}
		return map;
	}

	//配置更新
	public JSONObject systemConfigUpdate(JSONObject params)  {
		//初始数据
		DBUtil     dbUtil = new DBUtil();
		JSONObject map    = new JSONObject();
		map.put("resultFlag",false);
		map.put("resultMsg","操作失败");

		//参数
		String key    = params.getString("key");
		String value      = params.getString("value");

		//sql
		String sqlUpdate = "update system_configuration set v='"+value+"',update_time=NOW() where k='"+key+"'";

		try {
			Integer i = dbUtil.executeUpdateTOnum(sqlUpdate);
			//是否有更新成功，不管多少条,反正只取一条
			if(i>0){
				map.put("resultFlag",true);
				map.put("resultMsg","更新成功");
				logger.info("更改了系统配置：{}",sqlUpdate);
			}
		} catch (Exception ex) {

		} finally {
			dbUtil.closeNoErr();
		}

		return map;
	}


	/**
	 * 读取LEO价格
	 * @param params
	 * @return
	 */
	public JSONObject getLoePrice(JSONObject params) {
		JSONObject map = new JSONObject();
		map.put("resultFlag", false);
		DBUtil dbUtil = new DBUtil();

		//执行
		try {
			String sql = "select v from system_configuration where (k='leo_to_usdt') ";
			String issueLeoNubmer = ResultSetToJavaBean.copyJsonSingleQuery(dbUtil, sql).getString("v");

			map.put("data", issueLeoNubmer);
			map.put("resultFlag", true);
			map.put("resultMsg", "查询成功");
		} catch (Exception e) {
			map.put("resultMsg", "查询失败");
		} finally {
			dbUtil.closeNoErr();
		}
		return map;
	}


}
